package com.jkgl.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.jkgl.enums.ReportEnum;
import com.jkgl.mail.EmailUtil;
import com.jkgl.result.Report;
import com.jkgl.rsa.ASEUtils;
import com.jkgl.service.SendService;
import com.jkgl.stream.MailSenderObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.stereotype.Service;

import java.beans.Encoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Service
@RequiredArgsConstructor
@RefreshScope
@Slf4j
public class SendServiceImpl implements SendService {
    private final EmailUtil emailUtil;

    @Value("${base.addr}")
    private String adminAddr;
    @Value("${base.url}")
    private String baseUrl;

    @Override
    public boolean sendError(ObjectRecord<String, String> message) {
           return sendError(message.getId(),message.getValue());
    }

    @Override
    public boolean sendError(Object object,String mess) {
        try {
            log.info("发生错误，发送邮件提醒");
            emailUtil.sendText("发生异常 id:\n "+object,mess,adminAddr);
        }catch (Exception e){
            log.error("发送异常信息时异常==={}",e.getMessage());
            return false;
        }
        return true;
    }


    @Override
    public boolean sendNoticeToUser(ObjectRecord<String, String> message) {
        MailSenderObject object = JSON.to(MailSenderObject.class, message.getValue());
        try {
            log.info("发送运动提醒");
            emailUtil.sendText("运动报告提醒",object.getObj().toString(), object.getMess());
        }catch (Exception e){
            log.error("发送运动提醒异常==={}",e.getMessage());
            sendError(e,"发送运动提醒异常");
            return false;
        }
        return true;
    }

    @Override
    public boolean sendUserURL(ObjectRecord<String, String> message) {
//        TODO 字符串做用户id -周报日报类型 -date时间戳自己生成  完了 记得写对应接口
        try{
        Report report = JSON.to(Report.class, message.getValue());
        log.info("report========{}",report);
        String userId = report.getUserId().toString();
        String email = report.getEmail();
        ReportEnum reportEnum = report.getReportEnum();
        String userName = report.getUserName();
        Integer dataSign = report.getDataSign();
        if(dataSign == 1){
            emailUtil.sendText("昨日数据未及时登记！！",LocalDate.now().minusDays(1) + "日数据未及时登记 ",email);
        }
        log.info("发送报告链接:--=={}",userId);
//        返回字符串
//        每天晚上十二点执行---为第二天--解密默认见一天
        String format = String.format("%s|%s", userId, LocalDate.now());
//            生成密钥
        String encrypt = ASEUtils.encrypt(format);
//        Encoder 加码  Decoder解码
            encrypt = URLEncoder.encode(encrypt, StandardCharsets.UTF_8);
            if(ReportEnum.DAY.equals(reportEnum)) {
                String url =baseUrl+"/reportDay?encrypt="+encrypt;
                emailUtil.sendText("快来接收 " + userName + " 的报告！！！",url,email);
            }else{
                String url =baseUrl+"/reportWeek?encrypt="+encrypt;
                emailUtil.sendText("周报来袭！！！",url,email);
            }
        }catch (Exception e){
            log.error("发送报告异常提醒{}",e.getMessage());
            sendError(e,"发送报告异常提醒");
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        DayOfWeek dayOfWeek = LocalDate.now().minusDays(8).getDayOfWeek();
        System.out.println(dayOfWeek);
    }
}
